<?php
/**
 * @title 36. 有效的数独
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String[][] $board
     * @return Boolean
     */
    function isValidSudoku($board) {
        /**
         * @since 2023-12-13 记录每行、每列、每个区域的元素
         */
        $rowNumArray = $colNumArray = $areaNumArray = [];

        /**
         * @since 2023-12-13 遍历元素
         */
        for($row=0; $row<9; $row++){
            /**
             * @since 2023-12-13 横向区域
             */
            $rowArea = intval($row/3);

            for($col=0; $col<9; $col++){
                /**
                 * @since 2023-12-13 纵向区域
                 */
                $colArea = intval($col/3);

                /**
                 * @since 2023-12-13 元素
                 */
                $num = $board[$row][$col];

                /**
                 * @since 2023-12-13 非数字不判断
                 */
                if($num == "."){
                    continue;
                } else {}

                /**
                 * @since 2023-12-13 row行是否重复
                 */
                if(isset($rowNumArray[$row][$num])){
                    return false;
                } else {
                    $rowNumArray[$row][$num] = 1;
                }

                /**
                 * @since 2023-12-13 col列是否重复
                 */
                if(isset($colNumArray[$col][$num])){
                    return false;
                } else {
                    $colNumArray[$col][$num] = 1;
                }

                /**
                 * @since 2023-12-13 区域是否重复
                 */
                if(isset($areaNumArray[$rowArea][$colArea][$num])){
                    return false;
                } else {
                    $areaNumArray[$rowArea][$colArea][$num] = 1;
                }
            }
        }

        /**
         * @return
         */
        return true;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.13_2.php
 */
$datas = [
    [
        [
            ["5","3",".",".","7",".",".",".","."]
            ,["6",".",".","1","9","5",".",".","."]
            ,[".","9","8",".",".",".",".","6","."]
            ,["8",".",".",".","6",".",".",".","3"]
            ,["4",".",".","8",".","3",".",".","1"]
            ,["7",".",".",".","2",".",".",".","6"]
            ,[".","6",".",".",".",".","2","8","."]
            ,[".",".",".","4","1","9",".",".","5"]
            ,[".",".",".",".","8",".",".","7","9"]
        ],
    ], // true
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->isValidSudoku(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
